﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using reminder_csh.Entities;

namespace reminder_csh {
    internal class WorkerManagerImpl : IWorkerManager, IDisposable
    {
        private MyDbContext db = new MyDbContext();

        public void CreatePerson(Worker worker)
        {
            if (worker == null)
                throw new NullReferenceException("worker is null");
            db.WorkerDbSet.Add(worker);
            db.SaveChanges();
        }

        public void UpdatePerson(Worker worker)
        {
            if (worker == null)
                throw new NullReferenceException("worker is null");
            db.Entry(worker).State = EntityState.Modified;
            db.SaveChanges();
        }

        public void DeletePerson(Worker worker)
        {
            if (worker == null) throw new NullReferenceException("worker is null");
            var id = worker.Id;
            var w = db.WorkerDbSet.Find(id);
            if (w == null) throw new NullReferenceException("worker not in db");
            db.WorkerDbSet.Remove(w);
            db.SaveChanges();
        }

        public List<Worker> FindAllPersons()
        {
            return db.WorkerDbSet.ToList();
        }

        public Worker FindPersonById(int id)
        {
            var w = db.WorkerDbSet.Find(id);
            return w;
        }

        
    


    public void Dispose()
        {
            db.Dispose();
        }
    }
}
